﻿Imports DotNetNuke.Entities.Portals
Imports System.Collections.Specialized
Imports System.Text

Namespace DotNetNuke.Modules.ActiveForums.Controls
    Public Class SecurityGrid
        Public Property PortalSettings() As PortalSettings
        Public ReadOnly Property PortalId() As Integer
            Get
                Return PortalSettings.PortalId
            End Get
        End Property
        Public Property [ReadOnly]() As Boolean
        Public Property ImagePath() As String
        Public Property Security() As PermissionInfo
        Public Property PermissionsId() As Integer
        Public Sub New(ps As PortalSettings, isReadOnly As Boolean, imgPath As String, sec As PermissionInfo, permId As Integer)
            PortalSettings = ps
            [ReadOnly] = isReadOnly
            ImagePath = imgPath
            Security = sec
            PermissionsId = permId
        End Sub
        Public Function GetNewGrid() As String
            'Roles
            Dim roles() As String = GetSecureObjectList(Security, 0).Split(CChar(";"))
            Dim roleIds(roles.Length - 2) As Integer
            Dim i As Integer = 0
            For i = 0 To roles.Length - 2
                If Not String.IsNullOrEmpty(roles(i)) Then
                    roleIds(i) = CInt(roles(i))
                End If
            Next
            Array.Sort(roleIds)
            Dim tmp As String = String.Empty
            For Each n As Integer In roleIds
                tmp &= n.ToString & ";"
            Next
            Dim pl As New List(Of PermissionInfo)

            'litNewGrid.Text = "Roles:" & tmp

            'litNewGrid.Text &= "<br />RolesNames:" & Permissions.GetRolesNVC(tmp)
            Dim nvc As NameValueCollection = Permissions.GetRolesNVC(tmp, PortalId)
            For Each key As String In nvc.AllKeys
                Dim pi As New PermissionInfo
                With pi
                    .ObjectId = key
                    .ObjectName = nvc.Item(key)
                    .Type = ObjectType.RoleId
                End With
                pl.Add(pi)
            Next

            'Users
            Dim users As String = GetSecureObjectList(Security, 1)
            Dim userNames As String = String.Empty
            If Not String.IsNullOrEmpty(users) Then
                Dim uc As New DotNetNuke.Entities.Users.UserController
                For Each uid As String In users.Split(CChar(";"))
                    If Not String.IsNullOrEmpty(uid) Then
                        Dim u As DotNetNuke.Entities.Users.UserInfo = uc.GetUser(PortalId, CInt(uid))
                        If Not u Is Nothing Then
                            Dim pi As New PermissionInfo
                            With pi
                                .ObjectId = u.UserID.ToString
                                .ObjectName = u.DisplayName
                                .Type = ObjectType.UserId
                            End With
                            pl.Add(pi)
                        End If

                    End If
                Next
            End If
            'Groups
            Dim groups As String = GetSecureObjectList(Security, 2)
            If Not String.IsNullOrEmpty(groups) Then
                For Each g As String In groups.Split(CChar(";"))
                    If Not String.IsNullOrEmpty(g) Then
                        Dim uc As New Social
                        Dim gType As String = g.Split(CChar(":"))(1)
                        Dim groupId As Integer = CInt(g.Split(CChar(":"))(0))
                        Dim groupName As String = uc.ActiveSocialGetGroup(groupId)
                        If Not String.IsNullOrEmpty(groupName) Then
                            Dim pi As New PermissionInfo
                            With pi
                                .ObjectId = g
                                .ObjectName = groupName
                                .Type = ObjectType.GroupId
                            End With
                            pi.ObjectName &= "-" & uc.ActiveSocialGetGroupType(CInt(gType), PortalId)
                            pl.Add(pi)
                        End If
                    End If
                Next
            End If

            Dim grid(pl.Count, 27) As String
            i = 0
            For Each pi As PermissionInfo In pl
                grid(i, 0) = pi.ObjectId
                grid(i, 1) = pi.ObjectName
                grid(i, 2) = CStr(pi.Type)
                grid(i, 3) = CStr(PermValue(pi.Type, pi.ObjectId, Security.View))
                grid(i, 4) = CStr(PermValue(pi.Type, pi.ObjectId, Security.Read))
                grid(i, 5) = CStr(PermValue(pi.Type, pi.ObjectId, Security.Create))
                grid(i, 6) = CStr(PermValue(pi.Type, pi.ObjectId, Security.Reply))
                grid(i, 7) = CStr(PermValue(pi.Type, pi.ObjectId, Security.Edit))
                grid(i, 8) = CStr(PermValue(pi.Type, pi.ObjectId, Security.Delete))
                grid(i, 9) = CStr(PermValue(pi.Type, pi.ObjectId, Security.Lock))
                grid(i, 10) = CStr(PermValue(pi.Type, pi.ObjectId, Security.Pin))
                grid(i, 11) = CStr(PermValue(pi.Type, pi.ObjectId, Security.Attach))
                grid(i, 12) = CStr(PermValue(pi.Type, pi.ObjectId, Security.Poll))
                grid(i, 13) = CStr(PermValue(pi.Type, pi.ObjectId, Security.Block))
                grid(i, 14) = CStr(PermValue(pi.Type, pi.ObjectId, Security.Trust))
                grid(i, 15) = CStr(PermValue(pi.Type, pi.ObjectId, Security.Subscribe))
                grid(i, 16) = CStr(PermValue(pi.Type, pi.ObjectId, Security.Announce))
                grid(i, 17) = CStr(PermValue(pi.Type, pi.ObjectId, Security.Tag))
                grid(i, 18) = CStr(PermValue(pi.Type, pi.ObjectId, Security.Categorize))
                grid(i, 19) = CStr(PermValue(pi.Type, pi.ObjectId, Security.Prioritize))

                grid(i, 20) = CStr(PermValue(pi.Type, pi.ObjectId, Security.ModApprove))
                grid(i, 21) = CStr(PermValue(pi.Type, pi.ObjectId, Security.ModMove))
                grid(i, 22) = CStr(PermValue(pi.Type, pi.ObjectId, Security.ModSplit))
                grid(i, 23) = CStr(PermValue(pi.Type, pi.ObjectId, Security.ModDelete))
                grid(i, 24) = CStr(PermValue(pi.Type, pi.ObjectId, Security.ModUser))
                grid(i, 25) = CStr(PermValue(pi.Type, pi.ObjectId, Security.ModEdit))
                grid(i, 26) = CStr(PermValue(pi.Type, pi.ObjectId, Security.ModLock))
                grid(i, 27) = CStr(PermValue(pi.Type, pi.ObjectId, Security.ModPin))


                i += 1
            Next
            Dim enumType As System.Type = GetType(SecureActions)
            Dim values As Array = [Enum].GetValues(enumType)
            Dim sb As New StringBuilder
            sb.Append("<table cellpadding=""0"" cellspacing=""0""><tr><td valign=""top""><div class=""afsecobjects""><table cellpadding=""0"" cellspacing=""0"" border=""0"">")
            sb.Append("<tr><td class=""afsecobjecthd"" colspan=""2"">" & Utilities.GetSharedResource("[RESX:SecureObjects]", True) & "</td></tr>")
            Dim tmpObjectName As String = String.Empty
            Dim secRows As Integer = 0
            For x As Integer = 0 To pl.Count - 1
                sb.Append("<tr><td style=""width:16px;""></td><td class=""afsecobject"" style=""white-space:nowrap;""><div class=""afsecobjecttxt"" title=""" & grid(x, 1) & """ onmouseover=""this.firstChild.style.display='';"" onmouseout=""this.firstChild.style.display='none';""><span style=""width:16px;height:16px;float:right;display:none;"">")
                If (CInt(grid(x, 2)) = 0 AndAlso CInt(grid(x, 0)) > 0) Or CInt(grid(x, 2)) > 0 Then
                    If Not [ReadOnly] Then
                        sb.Append("<img src=""" & ImagePath & "mini_del.gif"" alt=""Remove Object"" style=""cursor:pointer;z-index:10;"" class=""afminidel"" onclick=""securityDelObject(this,'" & grid(x, 0) & "'," & grid(x, 2) & "," & permissionsId & ");"" />")
                    End If
                End If
                sb.Append("</span>" & grid(x, 1))
                sb.Append("</div></td></tr>")
            Next
            sb.Append("</table></div></td><td valign=""top"" width=""94%""><div class=""afsecactions"" style=""overflow-x:auto;overflow-y:hidden;"">")
            'litNewObjects.Text = sb.ToString
            'sb = New StringBuilder

            sb.Append("<table cellpadding=0 cellspacing=0 border=0 width=""100%"" id=""tblSecGrid"">")
            sb.Append("<tr>")
            Dim keyText As String = String.Empty
            For td As Integer = 3 To 27
                keyText = Convert.ToString([Enum].Parse(enumType, values.GetValue(td - 3).ToString()))
                If keyText.ToLowerInvariant = "block" Then
                    sb.Append("<td class=""afsecactionhd"" style=""display:none;"">")
                Else
                    sb.Append("<td class=""afsecactionhd"">")
                End If


                sb.Append(keyText)
                sb.Append("</td>")
            Next
            sb.Append("</tr>")
            For x As Integer = 0 To pl.Count - 1
                sb.Append("<tr onmouseover=""this.className='afgridrowover'"" onmouseout=""this.className='afgridrow'"">")
                For r As Integer = 3 To 27
                    keyText = Convert.ToString([Enum].Parse(enumType, values.GetValue(r - 3).ToString()))
                    Dim bState As Boolean = CBool(grid(x, r)) 'Permissions.HasPermission(ForumID, Integer.Parse(dr("ObjectId").ToString), key, Integer.Parse(dr("SecureType").ToString), dt)
                    Dim sState As String = "<img src=""" & ImagePath & "admin_stop.png"" alt=""Disabled"" />"
                    If bState Then
                        sState = "<img src=""" & ImagePath & "admin_check.png"" alt=""Enabled"" />"
                    End If
                    If keyText.ToLowerInvariant = "block" Then
                        sb.Append("<td class=""afsecactionelem"" style=""text-align:center;display:none;"">")
                    Else
                        sb.Append("<td class=""afsecactionelem"" style=""text-align:center;"">")
                    End If

                    sb.Append("<div class=""afsectoggle"" id=""" & grid(x, 0) & grid(x, 2) & keyText & """ ")
                    If Not [ReadOnly] Then
                        sb.Append("onclick=""securityToggle(this," & PermissionsId & ",'" & grid(x, 0) & "','" & grid(x, 1) & "'," & grid(x, 2) & ",'" & keyText & "');""")
                    End If
                    sb.Append(">" & sState & "</div></td>")


                Next
                sb.Append("</tr>")
            Next
            sb.Append("</table></div></td></tr></table>")
            Return sb.ToString()

        End Function
        Private Function GetSecureObjectList(ByVal s As PermissionInfo, ByVal objectType As Integer) As String
            Dim roleObjects As String = String.Empty

            roleObjects = GetObjFromSecObj(s.Announce, objectType, roleObjects)
            roleObjects = GetObjFromSecObj(s.Attach, objectType, roleObjects)
            roleObjects = GetObjFromSecObj(s.Categorize, objectType, roleObjects)
            roleObjects = GetObjFromSecObj(s.Create, objectType, roleObjects)
            roleObjects = GetObjFromSecObj(s.Delete, objectType, roleObjects)
            roleObjects = GetObjFromSecObj(s.Edit, objectType, roleObjects)
            roleObjects = GetObjFromSecObj(s.Lock, objectType, roleObjects)
            roleObjects = GetObjFromSecObj(s.ModApprove, objectType, roleObjects)
            roleObjects = GetObjFromSecObj(s.ModDelete, objectType, roleObjects)
            roleObjects = GetObjFromSecObj(s.ModEdit, objectType, roleObjects)
            roleObjects = GetObjFromSecObj(s.ModLock, objectType, roleObjects)
            roleObjects = GetObjFromSecObj(s.ModMove, objectType, roleObjects)
            roleObjects = GetObjFromSecObj(s.ModPin, objectType, roleObjects)
            roleObjects = GetObjFromSecObj(s.ModSplit, objectType, roleObjects)
            roleObjects = GetObjFromSecObj(s.ModUser, objectType, roleObjects)
            roleObjects = GetObjFromSecObj(s.Pin, objectType, roleObjects)
            roleObjects = GetObjFromSecObj(s.Poll, objectType, roleObjects)
            roleObjects = GetObjFromSecObj(s.Prioritize, objectType, roleObjects)
            roleObjects = GetObjFromSecObj(s.Read, objectType, roleObjects)
            roleObjects = GetObjFromSecObj(s.Reply, objectType, roleObjects)
            roleObjects = GetObjFromSecObj(s.Subscribe, objectType, roleObjects)
            roleObjects = GetObjFromSecObj(s.Tag, objectType, roleObjects)
            roleObjects = GetObjFromSecObj(s.Trust, objectType, roleObjects)
            roleObjects = GetObjFromSecObj(s.View, objectType, roleObjects)





            Return roleObjects
        End Function
        Private Function GetObjFromSecObj(ByVal permSet As String, ByVal index As Integer, ByVal objects As String) As String
            If String.IsNullOrEmpty(permSet) Then
                permSet = PortalSettings.AdministratorRoleId & ";||||"
            End If
            Dim perms() As String = permSet.Split(CChar("|"))
            If Not perms(index) Is Nothing Then
                If Not String.IsNullOrEmpty(perms(index)) Then
                    For Each s As String In perms(index).Split(CChar(";"))
                        If Not String.IsNullOrEmpty(s) Then
                            If Array.IndexOf(objects.Split(CChar(";")), s) = -1 Then
                                objects &= s & ";"
                            End If
                        End If
                    Next
                End If
            End If

            Return objects
        End Function
        Private Function PermValue(ByVal objectType As Integer, ByVal objectId As String, ByVal permSet As String) As Boolean
            If String.IsNullOrEmpty(permSet) Then
                Return False
            Else
                Return Permissions.HasAccess(permSet.Split(CChar("|"))(objectType), objectId & ";")
            End If

        End Function
    End Class
End Namespace

